<template>
	<view class="content">
		<view class="header" @click.capture="maskFalse">
			<view class="back" @click="goBack">
				< </view>
					<!-- 搜索框 -->
					<input type="text" v-model="value" placeholder="请输入搜索内容" @input="intChange" />
			</view>


			<!-- 热门搜索列表 -->
			<view v-if="!songsList.length">
				<view class="title">
					热门搜索
				</view>
				<view class="hots">
					<view class="hot-item" v-for="(item,index) in list" :key="index" @click="changeList(item.first)">
						{{item.first}}
					</view>
				</view>
			</view>



			<!-- 搜索有数据时的列表 -->
			<!-- 相关歌手 -->
			<view v-if="obj.playlists">
				<view class="count">
					相关歌手
				</view>
				<view v-for="(item,index) in oneList" :key="index">
					<musicList :nameValue="'歌手:'+item.name" :img="item.picUrl" @click.native="tapOne(1,item.id)">
					</musicList>
				</view>
			</view>


			<!-- 相关歌单 -->
			<view v-if="obj.playlists" class="cards">
				<view class="count">
					相关歌单
				</view>
				<view v-for="(item,index) in twoList" :key="index">
					<musicList :nameValue="'歌单:'+item.name" :img="item.coverImgUrl" @click.native="tapOne(2,item.id)">
					</musicList>
				</view>
			</view>


			<!-- 所有相关歌曲 -->
			<view v-show="songsList!=''" class="songs">
				<view v-for="(item,index) in songsList" :key="index">
					<musicList :nameValue="item.name" :describe="item.artists[0].name + '-' + item.album.name"
						@click.native="tapOne(3,item,index)">
					</musicList>
				</view>
			</view>

			<!-- 播放器 -->
			<view v-show="onlyData!=''">
				<musicTabbar :listIndex="i" @listTrue="listTrue" :flag="flag" :mp3="mp3" :title.sync="val" :play="true"
					:list="songsList" @toMp3="toMp3" :desc="desc" :url="img"></musicTabbar>
			</view>

		</view>
</template>

<script>
	import musicTabbar from "../../components/musicTabbar/musicTabbar.vue"
	import musicList from '../../components/musicList/musicList.vue'
	import {
		getSearch,
		getSearchList,
		getSearchListOne,
		getMp
	} from " ../../api/search.js"
	export default {
		data() {
			return {
				list: [], // 热门搜索 
				value: '', //输入框 
				songsList: [], //搜索歌曲数据 
				oneList: [], // 相关歌手
				twoList: [], // 相关歌单
				time: null,
				obj: {},
				onlyData: [],
				mp3: '', // 播放的mp3文件
				val: '', // 需要传给子组件播放器的名字
				desc: '', // 描述
				img: '', // 图片
				flag: false ,// 控制显示隐藏播放列表
				i:0
			}
		},
		mounted() {
			getSearch().then(res => {
				this.list = res.data.result.hots
			})
		},
		methods: {
			// 隐藏播放列表
			maskFalse() {
				this.flag = false
			},
			// 点击展开播放列表
			listTrue() {
				this.flag = true
			},
			getList() {
				// 所有歌曲数据
				getSearchList(this.value).then(res => {
					this.songsList = res.data.result.songs
				})
				//相关歌手相关歌单
				getSearchListOne(this.value).then(res => {
					this.oneList = res.data.result.artists
					this.twoList = res.data.result.playlists
					this.obj = res.data.result
				})
			},
			// 输入框发生改变时
			intChange() {
				clearTimeout(this.time)

				this.time = setTimeout(() => {
					if (this.value != '') {
						this.getList()
					} else {
						uni.showToast({
							title: "输入框不能为空哦"
						})
					}
				}, 3000)
			},
			// 数据回填
			changeList(it) {
				this.value = it
				this.getList()
			},
			// 点击返回上一页面
			goBack() {
				uni.navigateBack()
			},
			tapOne(i, it,index) {
				this.i = index
				if (i == 1) { // 跳转相关歌手
					uni.navigateTo({
						url: `/pages/singerList/singerList?id=${it}`,
						animationType: 'pop-in',
						animationDuration: 2000
					})
				} else if (i == 2) { // 跳转相关歌单
					uni.navigateTo({
						url: `/pages/songList/songList?id=${it}`,
						animationType: 'pop-in',
						animationDuration: 2000
					})
				} else if (i == 3) {
					console.log(it)
					this.val = it.name;
					this.desc = it.album.name;
					this.img = it.artists[0].img1v1Url;
					getMp(it.id).then(res => {
						this.onlyData = res.data.data[0];
						this.mp3 = this.onlyData.url
					})
				}
			}
		},
		components: {
			musicList,
			musicTabbar
		}
	}
</script>

<style lang="scss">
	.songs {
		background: #F2F3F4;
	}

	.cards {
		background: #F2F3F4;
	}

	.header {
		height: 80rpx;
		line-height: 80rpx;
		display: flex;
		background: #d44439;
		color: #fff;

		.back {
			font-size: 60rpx;
		}

		input {
			width: 100%;
			margin: 0rpx 25rpx;
			color: #fff;
			height: 65rpx;
			line-height: 65rpx;
			padding-left: 10rpx;
			border-bottom: 1rpx solid #ccc;
		}
	}

	.content {
		width: 100vw;
		height: 100vh;
		background: #F2F3F4;
	}

	.title {
		color: #bba8a8;
		padding-top: 25rpx;
		margin-left: 16rpx;
	}

	.hots {
		margin-top: 10rpx;
		display: flex;
		flex-wrap: wrap;
	}

	.hot-item {
		padding: 2rpx 15rpx;
		border: 1rpx solid #ccc;
		margin: 10rpx 16rpx;
		border-radius: 15rpx;
		background: #fff;
	}
</style>
